/*
 * @Author: your name
 * @Date: 2020-12-09 14:23:48
 * @LastEditTime: 2020-12-09 16:39:45
 * @LastEditors: Please set LastEditors
 * @Description: 日期处理相关
 * @FilePath: \h5-zhuhaiyun20201209\js\zz\time\time.js
 */
(function(){
    window.zzTime = {
        // 补零
        az(num){
            try {
                return num<10?"0"+num:""+num;
            } catch (error) {
                return "---az---";
            }
        },
        //日期字符串转Date对象的时候("-" -> "/")兼容ios
        str2DateRight(timeStr) { 
            var realTimeStr;
            try {
                if (_.isNaN(Date.parse(timeStr))) {
                    realTimeStr = timeStr.replace(/-/g, '/');
                } else {
                    realTimeStr = timeStr;
                }
                return realTimeStr;
            } catch (error) {
                return "---str2DateRight---";
            }
        },
        swTrim(str) { //去首尾空格
            try {
                return str.replace(/^\s+|\s+$/g, '');
            } catch (error) {
                return "---swTrim---";
            }
        },
        trim(str) { //去所有空格
            try {
                return str.replace(/^\s+|\s+$/g, '').replace(/\s+/g, '');
            } catch (error) {
                return "---trim---";
            }
        },
        // 获取一天的毫秒数
        oneDaySecs(){
            try {
                return +new Date(24*60*60*1000);
            } catch (error) {
                return "---oneDaySecs---";
            }
        },

        // 将Date对象转成yyyy-mm-dd
        date2yyyymmdd(date){
            try {
                var yearVal = date.getFullYear();
                var monthVal = date.getMonth() + 1;
                var dayVal = date.getDate();
                return this.az(yearVal) + '-' + this.az(monthVal) + '-' + this.az(dayVal);
            } catch (error) {
                return "---date2yyyymmdd---"
            }
        },

        // 将Date对象转成yyyy-mm-dd hh:mm:ss
        date2yyyymmddhhmmss(date){
            try {
                var yearVal = date.getFullYear();
                var monthVal = date.getMonth() + 1;
                var dayVal = date.getDate();
                var hVal = date.getHours();
                var minuVal = date.getMinutes();
                var secVal = date.getSeconds();
                return this.az(yearVal) + '-' + this.az(monthVal) + '-' + this.az(dayVal) + ' ' + this.az(hVal) + ':' + this.az(minuVal) + ':' + this.az(secVal);
            } catch (error) {
                return "---date2yyyymmddhhmmss---"
            }
        }

    };
})();
